
#include "../../include/search.h"
#include "../../include/sort.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define STRSIZE 6

typedef enum words_ { hop, hat, tap, bat, tip, mop, mom, cat, zoo, wax, top, dip
}words_t; /* 12 elements */


static int compare_str(const void *key1, const void *key2)
{
	int ret = strcmp((const char *)key1, (const char *)key2);
	if (ret < 0)
		return -1;
	else if (ret > 0)
			return 1;
		else
			return 0;
}


int main(int argc, char *argv[])
{
	char src_arr[12][STRSIZE], tar_arr[12][STRSIZE];
	char *target;

	strcpy(src_arr[hop], "hop");
	strcpy(src_arr[hat], "hat");
	strcpy(src_arr[tap], "tap");
	strcpy(src_arr[bat], "bat");
	strcpy(src_arr[tip], "tip");
	strcpy(src_arr[cat], "cat");
	strcpy(src_arr[zoo], "zoo");
	strcpy(src_arr[wax], "wax");
	strcpy(src_arr[top], "top");
	strcpy(src_arr[dip], "dip");
	strcpy(src_arr[mop], "mop");
	strcpy(src_arr[mom], "mom");

	strcpy(tar_arr[0], "bat");
	strcpy(tar_arr[1], "top");
	strcpy(tar_arr[2], "xxx");

	fprintf(stdout, "src_array is \n");
	for (int i = 0; i <= dip; i++)
		fprintf(stdout, "src_arr[%d] = %s\n", i, (char *)src_arr[i]);


	fprintf(stdout, "after insert sort\n");
	issort(src_arr, 12, STRSIZE, compare_str);
	for (int i = 0; i <= dip; i++)
		fprintf(stdout, "src_arr[%d] = %s\n", i, (char *)src_arr[i]);

	target = (char *)&tar_arr[1];
	int ret = bisearch(src_arr, 12, STRSIZE, target, compare_str);
	if (ret < 0)
		fprintf(stdout, "can't find target %s\n", target);
	else
		fprintf(stdout, "find target %s at %d\n", target, ret);

	return 0;
}

